#include <my_head.h>

int main(int argc, const char *argv[]){
    if(3 != argc){
        printf("Usage : %s <IP> <PORT>\n", argv[0]);
        return -1;
    }
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(-1 == sockfd)
        ERR_LOG("socket error");
    // 填充广播结构体
    struct sockaddr_in serveraddr;
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(atoi(argv[2]));
    // 要填广播地址  当前网段最大的IP地址 或者 255.255.255.255
    serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
    socklen_t serveraddr_len = sizeof(serveraddr);

    // 设置允许发送广播
    int flag = 1;
    if(-1 == setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &flag, sizeof(flag)))
        ERR_LOG("setsockopt error");

    char buff[128] = {0};
    while(1){
        fgets(buff, sizeof(buff), stdin);
        buff[strlen(buff)-1] = '\0';
        if(-1 == sendto(sockfd, buff, sizeof(buff), 0, \
                (struct sockaddr *)&serveraddr, serveraddr_len));
    }
    close(sockfd);

    return 0;
}